package pierwszy;

import java.util.Map;
import java.util.TreeMap;

import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class KsPDKPage{
	
	private WebDriver driver;
	private WebDriverWait wait;
	private Select siteType;
	
	protected static final Map<String, String> iconPlusOptionsMap = new TreeMap<String, String>(){
		{
			put("Aktualność", "Aktualności");
			put("Klienta", "Firmy");
			put("Pracownika / Administratora", "Pracownicy");
			put("Zlecenie zwykłe", "Zlecenia");
			put("Zobowiązanie wobec US", "Płatności");
			put("Składkę zus", "Płatności");
			put("Notkę", "Notki");
		}
	};

	protected static final Map<String, String> siteTypeMap = new TreeMap<String, String>(){
		{
			put("Portal", "ks_portal");
			put("Dotacje", "ks_dotacje");
			put("Ksiegowosc", "ks_ksiegowosc");
		}
	};
	
	KsPDKPage(WebDriver driver, WebDriverWait wait){
		this.driver = driver;
		this.wait = wait;
		
		if(!driver.getTitle().equals("Centrum Ksiegowości i Edukacji Finansowej - panel administratora")){
			throw new NoSuchWindowException("This is not the KsPortal page");
		}	
	}
	
	//LogOut Button
	public LogInPage LogOut(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.wylogujBtn));
		WebElement wyloguj = driver.findElement(KsPDKPageUiMapping.wylogujBtn);
		wyloguj.click();
		
		return new LogInPage(driver, wait);
	}
	
	//IconPlus Button
	public KsPDKPage selectIconPlus(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.ikonkaPlusBtn));
		WebElement ikonkaPlus = driver.findElement(KsPDKPageUiMapping.ikonkaPlusBtn);
		ikonkaPlus.click();
		
		return this;
	}
	
	public KsPDKPage closeIconPlus(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.zamknijIkonkaPlusBtn));
		WebElement zamknijIkonkaPlus = driver.findElement(KsPDKPageUiMapping.zamknijIkonkaPlusBtn);
		zamknijIkonkaPlus.click();
		
		return this;
	}
	
	public NotePage submitNote(){
		selectIconPlus();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.notkaLink));
		WebElement notka = driver.findElement(KsPDKPageUiMapping.notkaLink);
		notka.click();
		
		return new NotePage(driver, wait);
	}
	
	
	//Site List
	public KsPDKPage selectSiteList(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.siteTypeSelect));
		WebElement siteList = driver.findElement(KsPDKPageUiMapping.siteTypeSelect);
		siteList.click();
		
		return this;
	}
	
	
	public KsPPage chooseKsPortal(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.siteTypeSelect));
		siteType = new Select(driver.findElement(KsPDKPageUiMapping.siteTypeSelect));
		siteType.selectByVisibleText(KsPDKPage.siteTypeMap.get("Portal"));
		
		return new KsPPage(driver, wait);
	}
	
	
	public KsDPage chooseKsDotacje(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.siteTypeSelect));
		siteType = new Select(driver.findElement(KsPDKPageUiMapping.siteTypeSelect));
		siteType.selectByVisibleText(KsPDKPage.siteTypeMap.get("Dotacje"));

		return new KsDPage(driver, wait);
	}	
	
	
	public KsKPage chooseKsKsiegowosc(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.siteTypeSelect));
		siteType = new Select(driver.findElement(KsPDKPageUiMapping.siteTypeSelect));
		siteType.selectByVisibleText(KsPDKPage.siteTypeMap.get("Ksiegowosc"));

		return new KsKPage(driver, wait);
	}
	
	//Option List
	public KsPDKPage selectOptionList(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.optionList));
		WebElement optionList = driver.findElement(KsPDKPageUiMapping.optionList);
		optionList.click();
		
		return this;
	}
	
	//Select module
	public KsPModulesPage selectKsPModules(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.chooseModuleBtn));
		WebElement ksPortalModules = driver.findElement(KsPDKPageUiMapping.chooseModuleBtn);
		ksPortalModules.click();
		
		return new KsPModulesPage(driver, wait);
	}
	
	public KsKModulesPage selectKsKModules(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.chooseModuleBtn));
		WebElement ksDotacjeModules = driver.findElement(KsPDKPageUiMapping.chooseModuleBtn);
		ksDotacjeModules.click();
		
		return new KsKModulesPage(driver, wait);
	}
	
	public KsDModulesPage selectKsDModules(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.chooseModuleBtn));
		WebElement ksKsiegowoscModules = driver.findElement(KsPDKPageUiMapping.chooseModuleBtn);
		ksKsiegowoscModules.click();
		
		return new KsDModulesPage(driver, wait);
	}
	
	
	//getter
	public WebElement getIconPlusText(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.ikonkaPlusText));
		
		return driver.findElement(KsPDKPageUiMapping.ikonkaPlusText);
	}
	
	public WebElement getPortalTypeText(){
		wait.until(ExpectedConditions.presenceOfElementLocated(KsPDKPageUiMapping.siteTypeSelectText));
		
		return driver.findElement(KsPDKPageUiMapping.siteTypeSelectText);
	}
}